import asyncio
import functools
import time
from typing import Any, Callable
from utils.logging_config import setup_logging

logger = setup_logging()

def timeit(func: Callable[..., Any]) -> Callable[..., Any]:
    """耗时装饰器，支持同步 & 异步函数"""
    if asyncio.iscoroutinefunction(func):
        @functools.wraps(func)
        async def async_wrapper(*args, **kwargs):
            start = time.perf_counter()
            try:
                return await func(*args, **kwargs)
            finally:
                logger.info(f"[async] {func.__name__} 耗时: {time.perf_counter() - start:.6f} 秒")
        return async_wrapper
    else:
        @functools.wraps(func)
        def sync_wrapper(*args, **kwargs):
            start = time.perf_counter()
            try:
                return func(*args, **kwargs)
            finally:
                logger.info(f"[sync] {func.__name__} 耗时: {time.perf_counter() - start:.6f} 秒")
        return sync_wrapper